const  app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: "拼单群(4)",
    inputBarH: "8vh",
    textareaH: "4vh",
    textareaW: "66vw",
    showBtn: false,
    scrollTop: 10000000,
    input: null,
    autoFocus: false,
    messageData: [
      {
        id: 1,
        text: '这个方法可行吗？',
        userInfo: {
          userId: 1,
          avatar: "/static/image/two.jpg"
        }
      },
      {
        id: 2,
        text: '如果可以的话记得点个赞哈！如果可以的话记得点个赞哈！如果可以的话记得点个赞哈！如果可以的话记得点个赞哈！',
        userInfo: {
          userId: 2,
          avatar: "/static/image/first.png"
        }
      },
      {
        id: 3,
        text: '这个方法可行吗？',
        userInfo: {
          userId: 1,
          avatar: "/static/image/two.jpg"
        }
      },
      {
        id: 4,
        text: '如果可以的话记得点个赞哈！如果可以的话记得点个赞哈！如果可以的话记得点个赞哈！如果可以的话记得点个赞哈！',
        userInfo: {
          userId: 2,
          avatar: "/static/image/first.png"
        }
      },
      {
        id: 5,
        text: '这个方法可行吗？',
        userInfo: {
          userId: 1,
          avatar: "/static/image/two.jpg"
        }
      },
      {
        id: 6,
        text: '如果可以的话记得点个赞哈！如果可以的话记得点个赞哈！如果可以的话记得点个赞哈！如果可以的话记得点个赞哈！',
        userInfo: {
          userId: 2,
          avatar: "/static/image/first.png"
        }
      },
      {
        id: 7,
        text: '这个方法可行吗？',
        userInfo: {
          userId: 1,
          avatar: "/static/image/two.jpg"
        }
      },
      {
        id: 8,
        text: '如果可以的话记得点个赞哈！如果可以的话记得点个赞哈！如果可以的话记得点个赞哈！如果可以的话记得点个赞哈！',
        userInfo: {
          userId: 2,
          avatar: "/static/image/first.png"
        }
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    wx.setNavigationBarTitle({
      title: that.data.title
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.hideHomeButton();  //隐藏home/返回主页按钮
  },

  handleLineChange(e){
    let lineCount= e.detail.lineCount
    let that = this
    if(lineCount > 0 && lineCount < 10 ){
      that.setData({
        inputBarH: (8 + (lineCount - 1) * 2.5) + "vh",
        textareaH: (4 + (lineCount - 1) * 2.5) + "vh"
      })
    }
  },
  handleFocus(){
    // 回到底部
    this.setData({
      scrollTop: 10000000
    })
  },
  handleInput(e){
    let that = this
    let input = e.detail.value
    let showBtn = that.data.showBtn
    this.data.input = input
    if(input.length > 0 && !showBtn){
      that.setData({
        textareaW: 55 + "vw",
        showBtn: true
      })
    }
    if(input.length < 1){
      that.setData({
        textareaW: 66 + "vw",
        showBtn: false
      })
    }
  },
  handleSubmit(){
    let that = this
    let input = that.data.input
    let messageData = that.data.messageData
    let object = {
      id: messageData.length + 1,
      text: input,
      userInfo: app.data.userInfo
    }
    // that.setData({
    //   messageData: messageData.push(object)
    // })
    messageData.push(object)
    that.setData({
      messageData: messageData,
    })
    that.setData({
      scrollTop: 10000000,
      input: null,
      textareaW: 66 + "vw",
      showBtn: false,
      autoFocus: true
    })

  }

})